package logic

import (
	"context"
	"fmt"
	"models"

	"shop_srv/internal/svc"
	"shop_srv/shop_srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *shop_srv.UserLoginRequest) (*shop_srv.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	if err := models.DB.Where("account = ?", in.Account).Limit(1).Find(&user).Error; err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, fmt.Errorf("账号未注册")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("账号或密码错误")
	}
	return &shop_srv.UserLoginResponse{
		UserId: int64(user.ID),
	}, nil
}
